package net.ksic.uridongne;

import net.ksic.common.ImageHelper;
import net.ksic.common.NetworkManager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class PicActivity extends Activity implements OnClickListener {

	/**
	 * intent값 확인 TextView tv1 = null; TextView tv2 = null;
	 * 
	 * @Override protected void onCreate(Bundle savedInstanceState) {
	 *           super.onCreate(savedInstanceState);
	 *           setContentView(R.layout.activity_pic);
	 * 
	 *           tv1 = (TextView) findViewById(R.id.textView1); tv2 = (TextView)
	 *           findViewById(R.id.textView2);
	 * 
	 *           Intent fromIntent = getIntent(); String send_message =
	 *           fromIntent.getStringExtra("SEND_MESSAGE"); String address =
	 *           fromIntent.getStringExtra("ADDRESS");
	 *           tv1.setText(send_message); tv2.setText(address); }
	 */

	ImageView photo = null;
	String send_message = null, addr1 = null, addr2 = null, gpslati = null,
			gpslong = null, imgPath = null;

	Bitmap bmp = null;

	Button home = null, takepic = null, album = null, clist = null, group = null,
			cancel = null, submit = null;

	EditText itemidx = null, bizcd = null, subjectcd = null, locgpslati = null,
			locgpslong = null, locaddr1 = null, locaddr2 = null,
			telnumber = null, comment = null, photopath = null, useyn = null,
			stdate = null;
	
	String RESULT = "";
			
	NetworkManager networkManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pic);

		networkManager = new NetworkManager();
		
		Intent fromIntent = getIntent();
		send_message = fromIntent.getStringExtra("SEND_MESSAGE");
		addr1 = fromIntent.getStringExtra("locaddr1");
		addr2 = fromIntent.getStringExtra("locaddr2");
		gpslati = fromIntent.getStringExtra("locgpslati");
		gpslong = fromIntent.getStringExtra("locgpslong");

		Log.i("send_message", send_message);
		Log.i("addr1", addr1);
		Log.i("addr2", addr2);
		Log.i("gpslati", gpslati);
		Log.i("gpslong", gpslong);

		setButton();

		photo = (ImageView) findViewById(R.id.photo);

		if (send_message.equals("camera")) {
			imgPath = ImageHelper.getInstance().getNewPhotoPath();

			Intent cIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			cIntent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.parse("file://" + imgPath));

			startActivityForResult(cIntent, 100);
		} else if (send_message.equals("album")) {
			Intent aIntent = new Intent(Intent.ACTION_GET_CONTENT);
			aIntent.setType("image/*");

			startActivityForResult(aIntent, 100);
		} else {
			Intent mIntent = new Intent(this, MapActivity.class);
			startActivity(mIntent);
		}
	}

	public void setButton() {
		home = (Button) findViewById(R.id.home);
		takepic = (Button) findViewById(R.id.takepic);
		album = (Button) findViewById(R.id.album);
		clist = (Button) findViewById(R.id.clist);
		group = (Button) findViewById(R.id.group);
		cancel = (Button) findViewById(R.id.cancel);
		submit = (Button) findViewById(R.id.submit);

		itemidx = (EditText) findViewById(R.id.itemidx);
		bizcd = (EditText) findViewById(R.id.bizcd);
		subjectcd = (EditText) findViewById(R.id.subjectcd);
		locgpslati = (EditText) findViewById(R.id.locgpslati);
		locgpslong = (EditText) findViewById(R.id.locgpslong);
		locaddr1 = (EditText) findViewById(R.id.locaddr1);
		locaddr2 = (EditText) findViewById(R.id.locaddr2);
		telnumber = (EditText) findViewById(R.id.telnumber);
		comment = (EditText) findViewById(R.id.comment);
		photopath = (EditText) findViewById(R.id.photopath);
		useyn = (EditText) findViewById(R.id.useyn);
		stdate = (EditText) findViewById(R.id.stdate);

		home.setOnClickListener(this);
		takepic.setOnClickListener(this);
		album.setOnClickListener(this);
		clist.setOnClickListener(this);
		group.setOnClickListener(this);
		cancel.setOnClickListener(this);
		submit.setOnClickListener(this);

		Log.i("버튼셋업", "완료");
	}

	// bitmap 객체는 Activity가 종료되더라도 메모리에 계속 남아 있기 때문에,
	// Activity 종료시 반드시 직접 객체를 삭제해 주어야 한다.
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (bmp != null) {
			bmp.recycle();
			bmp = null;
		}
	}

	@Override
	public void onActivityResult(int activityNumber, int resultCode, Intent data) {
		if (activityNumber == 100) {
			if (resultCode == Activity.RESULT_OK) {
				if (send_message.equals("camera")) {
					// 촬영한 사진의 갤러리 등록
					ImageHelper.getInstance().addGallery(this, imgPath);
				} else if (send_message.equals("album")) {
					Uri photoUri = data.getData();

					imgPath = ImageHelper.getInstance().getFilePathFromUri(
							this, photoUri);
					android.util.Log.d("File_PATH", imgPath);
				} else {
					Intent mIntent = new Intent(this, MapActivity.class);
					startActivity(mIntent);
				}
				// 이미지 표시 해제
				photo.setImageBitmap(null);

				// 만약, 기존에 보여지던 이미지가 있다면, 객체 삭제
				if (bmp != null) {
					bmp.recycle();
					bmp = null;
				}

				// 썸네일 받아오기 (파일경로, 긴 축의 최대 px값)
				bmp = ImageHelper.getInstance().getThumb(imgPath, 800);

				// 이미지 뷰에 출력
				photo.setImageBitmap(bmp);

				locgpslati.setText(gpslati);
				locgpslong.setText(gpslong);
				locaddr1.setText(addr1);
				locaddr2.setText(addr2);
				photopath.setText(imgPath);
			}
		}
	}

	@Override
	public void onClick(View v) {
		String sendMsg = "";

		Intent intent = null;

		switch (v.getId()) {
		case R.id.cancel:
		case R.id.home:
			sendMsg = "home";
			Toast.makeText(this, "첫화면", Toast.LENGTH_SHORT).show();
			intent = new Intent(this, MapActivity.class);
			break;
		case R.id.takepic:
			sendMsg = "camera";
			Toast.makeText(this, "카메라", Toast.LENGTH_SHORT).show();
			intent = new Intent(this, PicActivity.class);
			break;
		case R.id.album:
			sendMsg = "album";
			Toast.makeText(this, "앨범", Toast.LENGTH_SHORT).show();
			intent = new Intent(this, PicActivity.class);
			break;
		case R.id.clist:
			sendMsg = "clist";
			Toast.makeText(this, "컨텐츠", Toast.LENGTH_SHORT).show();
			intent = new Intent(this, ContentsActivity.class);
			break;
		case R.id.group:
			sendMsg = "group";
			Toast.makeText(this, "커뮤니티", Toast.LENGTH_SHORT).show();
			intent = new Intent(this, GroupActivity.class);
			break;			
		case R.id.submit:
			sendMsg = "submit";
			
			String sitemidx = itemidx.getText().toString();
			String sbizcd = bizcd.getText().toString();
			String ssubjectcd = subjectcd.getText().toString();
			String slocgpslati = locgpslati.getText().toString();
			String slocgpslong = locgpslong.getText().toString();
			String slocaddr1 = locaddr1.getText().toString();
			String slocaddr2 = locaddr2.getText().toString();
			String stelnumber = telnumber.getText().toString();
			String scomment = comment.getText().toString();
			String sphotopath = photopath.getText().toString();
			String suseyn = useyn.getText().toString();
			String sstdate = stdate.getText().toString();

			if (sitemidx.equals("") || sbizcd.equals("")
					|| ssubjectcd.equals("") || slocgpslati.equals("")
					|| slocgpslong.equals("") || slocaddr1.equals("")
					|| slocaddr2.equals("") || stelnumber.equals("")
					|| scomment.equals("") || sphotopath.equals("")
					|| suseyn.equals("") || sstdate.equals("")) {

				Toast.makeText(this, "모든 정보를 입력해 주세요.", Toast.LENGTH_SHORT)
						.show();
			} else {
				
				String URL = "http://20.20.20.124:8080/MyServer/PicServer.jsp";
				//String URL = "http://20.20.20.34:8080/lookdom/testpic.do";
			
				String MSG = sitemidx + "," + sbizcd + ","
						+ ssubjectcd + "," + slocgpslati + "&,"
						+ slocgpslong + "," + slocaddr1 + "," + slocaddr2
						+ "," + stelnumber + "," + scomment + "," + sphotopath
						+ "," + suseyn + ","
						+ sstdate;
				
				new sendWorker().execute(URL, MSG, sphotopath);						
			}
			break;			
		}
		
		intent.putExtra("SEND_MESSAGE", sendMsg);
		intent.putExtra("locaddr1", addr1);
		intent.putExtra("locaddr2", addr2);
		intent.putExtra("locgpslati", locgpslati+"");
		intent.putExtra("locgpslong", locgpslong+"");
		
		startActivity(intent);
	}
	
	private class sendWorker extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {

			RESULT = networkManager.picJSP(params[0], params[1], params[2]);		
			
			return RESULT;
		}
		
		@Override
		protected void onPostExecute(String result) {
			Log.i("RESULT-_-", RESULT);
			if(RESULT.equals("true")) {
				callMap();
			} else {
				callIntro();
			}
		}		
	}

	private void callIntro() {
		Intent intent = null;		
		intent = new Intent(this, IntroActivity.class);
		startActivity(intent);		
	}
	
	private void callMap() {
		Intent intent = null;
		intent = new Intent(this, MapActivity.class);
		startActivity(intent);		
	}
}